package com.ruoyi.demo.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.demo.service.JyDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;

/**
 * @author wxs
 * @description
 * @date 2023/8/1 9:58
 * @motto

 */
@RequestMapping("/visual/platform/demo")
@RestController
public class JyDemoController extends BaseController {
    @Autowired
    JyDemoService jyService;
    @Value("${jy.ip}")
    private String JY_IP;

    @Value("${jy.switch}")
    private String JY_SWITCH;
    //
    /**
     * 获取免密登录 url
     *
     */
    @PostMapping("password-free-login-url")
    public AjaxResult getPasswordFreeLoginURL(HttpServletRequest request , RestTemplate restTemplate )

    {
    /*
        步骤一 获取TGT（登录票据：有效期8小时）
         */

        // 设置请求头信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        // 构造请求体（参数）
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("username", "YGSJ");
        body.add("password", "04FA4605FC5A707EBFFACC824486ED95");
        String url="http://"+JY_IP+":11125/sso/v1/tickets";
        // 构造请求对象
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(body, headers);

        // 发送POST请求并获取响应

        ResponseEntity<String> response = restTemplate.postForEntity(url, httpEntity, String.class);

        // 获取响应体中的TGT
        String tgt = response.getBody();

        /*
        步骤二  获取ST（服务票据：有效期一分钟）
         */
        // 构造请求体（参数）
        MultiValueMap<String, String> body2 = new LinkedMultiValueMap<>();
        body2.add("service", "http://"+JY_IP+":9000/cas/validate");
        // 构造请求对象
        HttpEntity<MultiValueMap<String, String>> httpEntity2 = new HttpEntity<>(body2, headers);

        // 发送POST请求并获取响应
        ResponseEntity<String> response2 = restTemplate.postForEntity(url+"/"+tgt, httpEntity2, String.class);

        // 获取响应体中的TGT
        String st = response2.getBody();

        // 处理TGT并返回相应结果
        System.out.println(tgt);
        System.out.println(st);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("url", "http://"+JY_IP+":9000/cas/validate"+"?ticket="+st);
        ajax.put("code", 200);
        return ajax;
    }
//    {
//        String passwordFreeLoginUrl = jyService.getPasswordFreeLoginURL();
//        AjaxResult ajax = AjaxResult.success();
//        ajax.put("url", passwordFreeLoginUrl);
//        ajax.put("code", 200);
//        return ajax;
//    }
    /**
     * 获取接口认证token
     */
    @GetMapping("/getAccessToken")
    public String getAccessToken() {
        return jyService.getAccessToken();
    }
    /**
     * 刷新token
     */
    @GetMapping("/refreshToken")
    public String refreshToken() {
        return jyService.getNewAccessToken();
    }
    /**
     * 实时事件订阅
     */
    @PostMapping("/eventSubscriptions")
    public String eventSubscriptions(@RequestBody Map param) {
        return jyService.eventSubscriptions(param);
    }

    /**
     * 查看订阅事件
     */
    @GetMapping("/eventSubscriptions")
    public String selectEventSubscriptions() {
        return jyService.selectEventSubscriptions();
    }

    /**
     * 取消订阅
     */
    @DeleteMapping("/eventSubscriptions")
    public String deleteEventSubscriptions(@RequestBody Map param) {
        return jyService.deleteEventSubscriptions(param);
    }
    /**
     * 图片url地址转换
     */
    @PostMapping("/picAddrTrans")
    public String picAddrTransx(@RequestBody List<String> addrList) {
        System.out.println(addrList.toString());
        return jyService.picAddrTrans(addrList);
    }

    /**
     * 获取通道列表
     */

    @GetMapping("/getChannelListWithDevice")
    public String getChannelListWithDevice() {
        return jyService.getChannelListWithDevice();
    }

    /**
     * 获取告警回放录像URL
     */
    @GetMapping("getPlayRecordUrl")
    public String getPlayRecordUrl(String channel_code, String event_time, String event_id) {
        return jyService.getPlayRecordUrl(channel_code, event_time, event_id);
    }

    /**
     * 获取org_index和org_name
     */
    @GetMapping("getIndexAndName")
    public String getIndexAndName() {
        return jyService.getIndexAndName();
    }
    /**
     * 新增硬盘录像机
     */
    @PostMapping("addDVR")
    public String addDVR(@RequestBody Map param){
        return jyService.addDVR(param);
    }
    /**
     * 查询视频质量诊断结果
     */
    @GetMapping("getVideoDiagnosisResults")
    public String getVideoDiagnosisResults(String channel_name,  String start_time, Integer page_num, Integer page_size) {
        return jyService.getVideoDiagnosisResults(channel_name, start_time,  page_num, page_size);
    }
    /**
     * 最后一次诊断结果查询
     */
    @PostMapping("getLastVideoDiagnosisResult")
    public String getLastVideoDiagnosisResult(@RequestBody Map param) {
        return jyService.getLastVideoDiagnosisResult(param);
    }
    /**
     * 查询已安装的算法信息
     */
    @GetMapping("installed-algos")
    public String getInstalledAlgos() {
        return  jyService.getInstalledAlgos();
    }

    /**
     * 查询视频分析任务
     */
    @GetMapping("video-analysis-tasks")
    public String getVideoAnalysisTasks() {
        return  jyService.getVideoAnalysisTasks();
    }
    /**
     * 统计监控点在线率
     */
    @GetMapping("count-online-rate")
    public String countOnlineRate(String pageSize, String pageNum) {
        return jyService.countOnlineRate(pageSize, pageNum);
    }
    /**
     * 统计监控点达标率
     */
    @GetMapping("count-compliance-ratio")
    public String countComplianceRatio(String pageSize, String pageNum, String queryDate) {
        return jyService.countComplianceRatio(pageSize, pageNum, queryDate);
    }



    private Integer getSchemeIdOfFirstScheme(String schemeListJsonStr) {
        JSONArray jsonArray = JSONObject.parseObject(schemeListJsonStr).getJSONObject("data").getJSONArray("data");
        return jsonArray.getJSONObject(0).getIntValue("id");
    }

}
//
